package com.example.roundcornerprogressbar.slice;

import com.example.roundcornerprogressbar.ResourceTable;
import com.example.roundcornerprogressbar.view.progressbar.indeterminate.IndeterminateCenteredRoundCornerProgressBar;
import com.example.roundcornerprogressbar.view.progressbar.indeterminate.IndeterminateRoundCornerProgressBar;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.global.resource.ResourceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class IndeterminateAbilitySlice extends AbilitySlice {
    private HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,1, "RoundCorner:IndeterminateAbilitySlice");
    private IndeterminateRoundCornerProgressBar roundCornerProgressBar;
    private IndeterminateRoundCornerProgressBar roundCornerProgressBar2;
    private IndeterminateCenteredRoundCornerProgressBar centeredRoundCornerProgressBar;
    private IndeterminateCenteredRoundCornerProgressBar centeredRoundCornerProgressBar2;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_indeterminate);

        initView();
    }

    private void initView() {
        roundCornerProgressBar = (IndeterminateRoundCornerProgressBar) findComponentById(ResourceTable.Id_progressBarIndeterminate);
        if (null != roundCornerProgressBar) {
            setProgressBarColor(roundCornerProgressBar);
        }
        HiLog.info(label, "initView, roundCornerProgressBar = "+roundCornerProgressBar);
        roundCornerProgressBar2 = (IndeterminateRoundCornerProgressBar) findComponentById(ResourceTable.Id_progressBarIndeterminate2);
        if (null != roundCornerProgressBar2) {
            setProgressBarColor(roundCornerProgressBar2);
        }
        roundCornerProgressBar2.setAnimationSpeedScale(3);

        centeredRoundCornerProgressBar = (IndeterminateCenteredRoundCornerProgressBar) findComponentById(ResourceTable.Id_progressBarIndeterminateCentered);
        if (null != centeredRoundCornerProgressBar) {
            setProgressBarColor(centeredRoundCornerProgressBar);
        }
        centeredRoundCornerProgressBar.setAnimationSpeedScale((float) 0.5);

        centeredRoundCornerProgressBar2 = (IndeterminateCenteredRoundCornerProgressBar) findComponentById(ResourceTable.Id_progressBarIndeterminateCentered2);
        if (null != centeredRoundCornerProgressBar2) {
            setProgressBarColor(centeredRoundCornerProgressBar2);
        }
    }

    private void setProgressBarColor(IndeterminateRoundCornerProgressBar roundCornerProgressBar) {
        ResourceManager resManager = getResourceManager();
        try {
            roundCornerProgressBar.setProgressBackgroundColor(resManager.getElement(ResourceTable.Color_sample_progress_background).getColor());
            roundCornerProgressBar.setProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_primary).getColor());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setProgressBarColor(IndeterminateCenteredRoundCornerProgressBar roundCornerProgressBar) {
        ResourceManager resManager = getResourceManager();
        try {
            roundCornerProgressBar.setProgressBackgroundColor(resManager.getElement(ResourceTable.Color_sample_progress_background).getColor());
            roundCornerProgressBar.setProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_primary).getColor());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActive() {
        HiLog.info(label, "onActive");
        //super.onActive();
        roundCornerProgressBar.startIndeterminateAnimation();
    }

    @Override
    protected void onInactive() {
        HiLog.info(label, "onInactive");
        //super.onInactive();
        roundCornerProgressBar.stopIndeterminateAnimation();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
